package demo1;

import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * @description: 实现增删改
 * @author: apple
 * @date:  2022/1/9 4:45 下午
 * @version 1.0
 **/
public class PreparedStatementUpdateTest {


    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");


    @Test
    public void addTest1() throws IOException, SQLException, ParseException {


        String sql = "insert into student values(?,?,?,?)";

        final Date parse = format.parse("1992-10-19");
        java.sql.Date birthday = new java.sql.Date(parse.getTime());
        update(sql , 7 , "wenJian" , 29 ,birthday);
    }




    private void update( String sql , Object...params) throws SQLException, ParseException, IOException {
        final Connection connection = JDBCUtil.getConnection();
        final PreparedStatement preparedStatement = connection.prepareStatement(sql);
        for (int i = 0; i < params.length; i++) {
            preparedStatement.setObject(i+1, params[i]);
        }
        preparedStatement.execute();
        JDBCUtil.closeConnection(connection,preparedStatement);
        System.out.println("close...");
    }
}
